import os
import time

from utils import doc_util, read_config

cache_dict = {}

# 所有的dayk
day_all_ks_key = 'dakk'


# 有效期缓存
# 分钟
def set_cache_expire(key, data, expire: int):
    if expire <= 0:
        return
    cache_dict[key] = {'data': data, 'expire': expire, 'create_time': int(round(time.time()))}


# 删除缓存
def del_cache(key):
    if key in cache_dict.keys():
        cache_dict.pop(key)


# 获取缓存
def get_cache_expire(key):
    if key not in cache_dict.keys():
        return
    return cache_dict.get(key).get('data')


# 持久化文档
# 分钟
def set_doc_expire(key, data: str, expire: int):
    if expire <= 0:
        return
    cache_dict[key] = {'data': key, 'type': 'doc', 'expire': expire, 'create_time': int(round(time.time()))}
    doc_util.gen_new_doc(os.path.join(read_config.data_path, 'utils', 'cache'), key, [data])


# 获取持久化文档
def get_doc_expire(key):
    if key not in cache_dict.keys():
        return
    return doc_util.get_path_doc_info(os.path.join(read_config.data_path, 'utils', 'cache', key))[0]


# 手动删除文件和缓存
def del_doc_cache(key):
    if key in cache_dict.keys():
        cache_dict.pop(key)
    try:
        doc_util.del_doc(os.path.join(read_config.data_path, 'utils', 'cache', key))
    except Exception as e:
        print('无缓存文档')


def check_expire():
    del_cache_list = []
    for cache in cache_dict.keys():
        if 'expire' not in cache_dict.get(cache).keys():
            continue
        if int(round(time.time())) - cache_dict.get(cache).get('create_time') > \
                cache_dict.get(cache).get('expire') * 60:
            if 'type' in cache_dict.get(cache).keys():
                if cache_dict.get(cache).get('type') == 'doc':
                    doc_util.del_doc(os.path.join(read_config.data_path, 'utils', 'cache', cache))
            del_cache_list.append(cache)
    for cache in del_cache_list:
        cache_dict.pop(cache)


# 获取缓存
# key 外部的键
# code 内部缓存键
# data 内部缓存值
def set_cache(key, code, data):
    if key not in cache_dict.keys():  # 初始化一块缓存
        cache_dict[key] = {'cache_info': {"total": 0, 'del_flag': 2, 'keys1': [], 'keys2': []}}
    cur_cache = cache_dict[key]
    cache_info = cur_cache['cache_info']
    total = cache_info['total']
    self_check = 0
    pool_size = 10
    if total >= pool_size:
        del_flag = cache_info['del_flag']
        if del_flag == 1:
            del_list = cache_info['keys1']
            cache_info['keys1'] = []
            cache_info['del_flag'] = 2
        else:
            del_list = cache_info['keys2']
            cache_info['keys2'] = []
            cache_info['del_flag'] = 1
        for del_key in del_list:
            if del_key in cur_cache.keys():
                cur_cache.pop(del_key)
            else:
                self_check = 1
        cache_info['total'] = int(pool_size / 2)
    if cache_info['del_flag'] == 1:
        keys2 = cache_info['keys2']
        if len(keys2) >= int(pool_size / 2):
            keys1 = cache_info['keys1']
            keys1.append(code)
            cache_info['del_flag'] = 2
        else:
            keys2.append(code)
        cache_info['total'] += 1
    else:
        keys1 = cache_info['keys1']
        if len(keys1) >= int(pool_size / 2):
            keys2 = cache_info['keys2']
            keys2.append(code)
            cache_info['del_flag'] = 1
        else:
            keys1.append(code)
        cache_info['total'] += 1
    cur_cache['cache_info'] = cache_info
    cur_cache[code] = data
    if self_check == 1:
        print("缓存自校验" + code + key)
        keys = cache_info['keys1'] + cache_info['keys2']
        self_del_list = []
        for k in cur_cache.keys():
            if k != '000000' and (k not in keys) and k != 'cache_info':
                self_del_list.append(k)
        for k in self_del_list:
            cur_cache.pop(k)


# 获取缓存
# key 外部的键
# data 期望返回的格式
def get_cache(key, code, data):
    if key not in cache_dict.keys():
        return data
    cur_cache = cache_dict[key]
    if code not in cur_cache:
        return data
    return cur_cache[code]


if __name__ == '__main__':
    print(int(round(time.time())))
    for i in range(96):
        set_cache('a', str(i), i)
    print(get_cache('a', '95', 10000))
